<template>
  <div>
    <el-dialog title="添加客户" :visible="showDialog" @close="empty">
      <!-- form表单 -->
      <el-form ref="form" :rules="rules" :model="formDate" label-width="80px">
        <el-form-item label="经纪">
          <el-select v-model="formDate.agent" placeholder="全部经纪人" style="width:120px">
            <el-option v-for="item in jingJiRlist" :key="item.id" :label="item.name" :value="item.name">{{ item.name }}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="name" label="姓名">
          <el-input v-model="formDate.name" />
        </el-form-item>
        <el-form-item prop="email" label="邮箱">
          <el-input v-model="formDate.email" />
        </el-form-item>
        <el-form-item prop="phone" label="电话">
          <el-input v-model="formDate.phone" />
        </el-form-item>
        <el-form-item prop="wechat" label="微信">
          <el-input v-model="formDate.wechat" />
        </el-form-item>
        <el-form-item prop="desc" label="描述">
          <el-input v-model="formDate.desc" type="textarea" />
        </el-form-item>
        <!-- 上传框 -->
        <el-form-item label="封面">
          <upload ref="uploadImg1" />
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-form-item>
            <el-button size="small" type="primary" @click="clientFn">确定</el-button>
            <el-button size="small" @click="empty">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { addClient, editFn, getjingjiRList, getIdClient } from '@/api/clientAdmin'
import upload from '@/components/Uploadimgin/index.vue'
export default {
  components: {
    upload
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formDate: {
        agent: '', // 经纪人
        name: '',
        email: '',
        phone: '', // 描述
        wechat: '', // 微信
        desc: '', // 描述
        image: ''
      },
      jingJiRlist: [],
      rules: {
        name: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        desc: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
      }
    }
  },
  async created() {
    // 获取经纪人列表
    const res = await getjingjiRList({
    })

    this.jingJiRlist = res.data
  },
  methods: {
    // 点击确定
    async clientFn() {
    // 校验
      await this.$refs.form.validate()
      if (this.formDate.id) {
        this.formDate.image = this.$refs.uploadImg1.fileList[0].url
        // 有id 就是编辑
        await editFn(this.formDate)
      } else {
        if (this.$refs.uploadImg1.fileList) {
          this.formDate.image = this.$refs.uploadImg1.fileList[0].url
        }
        //   调用接口实现新增
        await addClient(this.formDate)
        // console.log(104, this.$refs.uploadImg1.fileList)
      }
      // 从新渲染页面 父组件监听 然后调用渲染函数
      this.$emit('addEvent')
      this.empty()
    },
    // 获取角色详情实现回显
    async  getIdClients(id) {
      const res = await getIdClient(id)
      this.formDate = res.data
      // 实现头像回显
      if (this.formDate.image) {
        this.$refs.uploadImg1.fileList.push({
          url: this.formDate.image
        })
      }
    },

    // 封装关闭清除数据等函数
    async empty() {
      // 关闭弹窗子传父
      this.$emit('update:showDialog', false)
      this.formDate = {
        agent: '', // 微信
        name: '',
        email: '', // 描述
        phone: '',
        wechat: '', // 电话
        desc: 0 // 经纪人
      }
      this.$refs.uploadImg1.fileList = []
      // 清空校验
      await this.$refs.form.resetFields()
    }
  }
}
</script>

<style>

</style>
